//
//  QTSearchViewController.m
//  qdqts
//
//  Created by jonn on 14-5-28.
//  Copyright (c) 2014年 seas.cheng. All rights reserved.
//

#import "QTSearchViewController.h"
#import "searchCategoryViewController.h"
#import "QTProductDetailViewController.h"
#import "ProdListTableViewController.h"
#import "QTProductListViewController.h"
#import "ProductRandomLabel.h"

@interface QTSearchViewController ()

@end

@implementation QTSearchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"搜索";
    self.searchBar.delegate=self;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"分类" style:UIBarButtonItemStyleBordered target:self action:@selector(category)];
    [self.navigationItem setRightBarButtonItem:rightItem];
    
//   Arthur, Kevin li has solved this problem, please do not spend same effort on same task
    
    //labelARy =[NSArray arrayWithObjects:@"电脑",@"电冰箱",@"电视机",@"空调",@"吸尘器",@"平板电脑",@"数码",@"音响",@"DVD",@"饮水机",@"油烟机",@"热水器",@"手机",@"洗衣机",nil];
    self.cv=[[CloudView alloc] initWithFrame:CGRectMake(0, 130, SCREEN_WIDTH, SCREEN_HEIGHT - 50)];
    
    self.cv.clickDelegate=self;
    self.cv.layer.borderColor=(__bridge CGColorRef)([UIColor clearColor]);
    [self.view addSubview:self.cv];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
    [self refreshkeywords];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
    [self.cv stopTimer];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)ClickLabel:(NSString *)str
{
    NSLog(@"%@",str);
    ((UISearchBar*)[self.view viewWithTag:1001]).text = str;
    [[self.view viewWithTag:1001] becomeFirstResponder];
}

- (void)category {
    searchCategoryViewController *vc = [[searchCategoryViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:nil action:nil];
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    [self.navigationItem setBackBarButtonItem:backItem];
}

-(BOOL)canBecomeFirstResponder
{
    return YES;
}


//Add the motionEnded or motionBegan method to your View Controller (模拟器中响应motionBegan, motionEnded，但真实机器iphone4中只能响应motionBegan):
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake)
    {
//        UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"title" message:@"摇一摇" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//        [servicesDisabledAlert show];
        [self.cv stopTimer];
        [self refreshkeywords];
    }
}


/*键盘搜索按钮*/
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    QTProductListViewController *productListViewController = [[QTProductListViewController alloc] initWithNibName:@"QTProductListViewController" bundle:nil];
    productListViewController.keyWord=searchBar.text;
    productListViewController.pageTitle=searchBar.text;
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:nil action:nil];
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    [self.navigationItem setBackBarButtonItem:backItem];
    [self.navigationController pushViewController:productListViewController animated:YES];
}

- (void)refreshkeywords
{
    [[QSClient instance]getSearchLabels:nil completionBlock:^(ProductRandomLabel *productRandomLabel, NSError *error)
     {
         NSInteger array_count = productRandomLabel.lables.count;
         NSString * temp;
         if (error) {
             [self showAlert:@"无法连接服务器，请检查网络"];
         }
         else if (labelARy == nil) {
             labelARy = [[NSMutableArray alloc]init];
         }
         else {
             [labelARy removeAllObjects];
         }
         for (int i=0; i < array_count;i++) {
             temp = [[productRandomLabel.lables objectAtIndex:i] objectForKey:@"lableName"];
             [labelARy addObject:temp];
         }
         if ([labelARy count] > 0)
         {
             [self.cv reloadData:labelARy];
         }
         else{
             NSLog(@"数据没来得及刷新！可以考虑在页面上加提示，不能空着屏幕");
             [self showAlert:@"无可用数据，重新加载？"];
         }

     }];
    
}


-(void)showAlert:(NSString *)str
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示"
                                                  message:[NSString stringWithFormat:str,nil]
                                                 delegate:self
                                        cancelButtonTitle:@"确定"
                                        otherButtonTitles:@"取消", nil];
    [alert show];
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) {
        [self refreshkeywords];
    }

}



@end
